package cn.app.android.sichuanxiaoke.com.xiaokeapp;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.activity.HomePHPActivity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.ClassifyFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.HomeFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.HomeFragmentShopcart;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.MineFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.NewHomeFragment;

public class MyAppHomeActivity extends AppCompatActivity {
    @BindView(R.id.HomeTabLayout)
    TabLayout mTabLayout;

    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;
    private ClassifyFragment classifyFragment;
    private MineFragment mineFragment;
    private HomeFragmentShopcart homeFragmentShopcart;
    private NewHomeFragment newHomeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_app_home);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        homeFragment=new HomeFragment();
        classifyFragment=new ClassifyFragment();
        mineFragment=new MineFragment();
        newHomeFragment=new NewHomeFragment();
        homeFragmentShopcart=new HomeFragmentShopcart();
        getCommitFragment(newHomeFragment);

        TabLayout.Tab tabHome = mTabLayout.newTab();
        TabLayout.Tab tabClass = mTabLayout.newTab();
        TabLayout.Tab tabShopCart = mTabLayout.newTab();
        TabLayout.Tab tabUser = mTabLayout.newTab();

        mTabLayout.addTab(tabHome.setIcon(R.drawable.home_activity_tab_home).setTag("tag1").setText("主页"));
        mTabLayout.addTab(tabClass.setIcon(R.drawable.home_activity_tab_home).setTag("tag2").setText("分类"));
        mTabLayout.addTab(tabShopCart.setIcon(R.drawable.home_activity_tab_home).setTag("tag3").setText("购物车"));
        mTabLayout.addTab(tabUser.setIcon(R.drawable.home_activity_tab_user).setTag("tag4").setText("我的"));
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if("tag1".equals(tab.getTag())) {
                    getCommitFragment(newHomeFragment);
                }else if ("tag2".equals(tab.getTag())) {
                    getCommitFragment(classifyFragment);
                } else if ("tag3".equals(tab.getTag())) {
                    getCommitFragment(homeFragmentShopcart);
                } else if ("tag4".equals(tab.getTag())) {
                    getCommitFragment(mineFragment);
                }

            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }



    private void getCommitFragment(Fragment fragment){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragmentContainer,fragment);

        transaction.commit();
    }
}
